package com.ruoyi.web.controller.business;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.BModel;
import com.ruoyi.system.domain.vo.BModelVo;
import com.ruoyi.system.service.BModelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 模型管理
 * @author yangxz
 **/
@RestController
@RequestMapping("/business/model")
public class ModelController extends BaseController {

    @Autowired
    BModelService modelService;

    /**
     * 获取模型列表
     */
    @PreAuthorize("@ss.hasPermi('dict:model:list')")
    @GetMapping("/list")
    public TableDataInfo list(BModel model)
    {
        startPage();
        List<BModelVo> list = modelService.selectModelList(model);
        return getDataTable(list);
    }

    /**
     * 根据模型Id获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('dict:model:query')")
    @GetMapping(value = "/{modelId}")
    public AjaxResult getInfo(@PathVariable Long modelId) {
        BModelVo modelVo = modelService.selectModelById(modelId);
        return success(modelVo);
    }

    /**
     * 新增模型
     */
    @PreAuthorize("@ss.hasPermi('dict:model:add')")
    @Log(title = "新增模型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody BModel model)
    {
        model.setCreateBy(getUsername());
        model.setCreateTime(new Date());
        return toAjax(modelService.save(model));
    }

    /**
     * 修改模型
     */
    @PreAuthorize("@ss.hasPermi('dict:model:edit')")
    @Log(title = "修改模型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody BModel model)
    {
        model.setUpdateBy(getUsername());
        model.setUpdateTime(new Date());
        return toAjax(modelService.updateById(model));
    }

    /**
     * 删除模型
     */
    @PreAuthorize("@ss.hasPermi('dict:model:remove')")
    @Log(title = "删除模型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{modelIds}")
    public AjaxResult remove(@PathVariable Long[] modelIds)
    {
        return toAjax(modelService.deleteModelByIds(modelIds));
    }
}
